﻿<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0031)http://www.css88.com/tool/ysjs/ -->
<!--************************************************************************--><!--* JavaScript Crunchinator Demo                                         *--><!--*                                                                      *--><!--* Copyright 2001 by Mike Hall                                          *--><!--************************************************************************--><HTML 
lang=en xml:lang="en" xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>JavaScript代码压缩-js代码压缩-压缩JS</TITLE>
<META content=text/html;charset=utf-8 http-equiv=Content-Type>
<SCRIPT type=text/javascript>//<![CDATA[

var literalStrings;  //临时变量.

function crunch(f) {

  var input, output;
  var i;

  // 获得输入里的代码,处理并显示输出

  f.elements.codeOut.value = "";
  f.elements.statusMsg.value = "进行中...";
  input = f.elements.codeIn.value;
  output = input;
  f.elements.statusMsg.value = "替换字符串...";
  output = replaceLiteralStrings(output);
  f.elements.statusMsg.value = "去掉注释...";
  output = removeComments(output);
  f.elements.statusMsg.value = "压缩空格...";
  output = compressWhiteSpace(output);
  f.elements.statusMsg.value = "连接字符串...";
  output = combineLiteralStrings(output);
  f.elements.statusMsg.value = "恢复字符串...";
  output = restoreLiteralStrings(output);
  f.elements.statusMsg.value = "完成.";
  f.elements.codeOut.value = output;

  // Compute sizes.

  f.elements.sizeIn.value   = input.length;
  f.elements.sizeOut.value  = output.length;
  f.elements.sizeDiff.value = input.length - output.length;
  if (input.length > 0)
    f.elements.pctOut.value = 100 -
      Math.round(output.length / input.length * 10000) / 100;

  return false;
}

function replaceLiteralStrings(s) {

  var i, c, t, lines, escaped, quoteChar, inQuote, literal;

  literalStrings = new Array();
  t = "";

  // Split script into individual lines.

  lines = s.split("\n");
  for (i = 0; i < lines.length; i++) {

    j = 0;
    inQuote = false;
    while (j <= lines[i].length) {
      c = lines[i].charAt(j);

      // If not already in a string, look for the start of one.

      if (!inQuote) {
        if (c == '"' || c == "'") {
          inQuote = true;
          escaped = false;
          quoteChar = c;
          literal = c;
        }
       else
         t += c;
      }

      // Already in a string, look for end and copy characters.

      else {
        if (c == quoteChar && !escaped) {
          inQuote = false;
          literal += quoteChar;
          t += "__" + literalStrings.length + "__";
          literalStrings[literalStrings.length] = literal;
        }
        else if (c == "\\" && !escaped)
          escaped = true;
        else
          escaped = false;
        literal += c;
      }
      j++;
    }
    t += "\n";
  }

  return t;
}

function removeComments(s) {

  var lines, i, t;

  // Remove '//' comments from each line.

  lines = s.split("\n");
  t = "";
  for (i = 0; i < lines.length; i++)
    t += lines[i].replace(/([^\x2f]*)\x2f\x2f.*$/, "$1");

  // Replace newline characters with spaces.

  t = t.replace(/(.*)\n(.*)/g, "$1 $2");

  // Remove '/* ... */' comments.

  lines = t.split("*/");
  t = "";
  for (i = 0; i < lines.length; i++)
    t += lines[i].replace(/(.*)\x2f\x2a(.*)$/g, "$1 ");

  return t;
}

function compressWhiteSpace(s) {

  // Condense white space.

  s = s.replace(/\s+/g, " ");
  s = s.replace(/^\s(.*)/, "$1");
  s = s.replace(/(.*)\s$/, "$1");

  // Remove uneccessary white space around operators, braces and parentices.

  //[\x21\x25\x26\x28\x29\x2a\x2b\x2c\x2d\x2f\x3a\x3b\x3c\x3d\x3e\x3f\x5b\x5d\x5c\x7b\x7c\x7d\x7e]
  //[!%&()*+,-/:;<=>?[]\{|}~]
  s = s.replace(/\s([\x21\x25\x26\x28\x29\x2a\x2b\x2c\x2d\x2f\x3a\x3b\x3c\x3d\x3e\x3f\x5b\x5d\x5c\x7b\x7c\x7d\x7e])/g, "$1");
  s = s.replace(/([\x21\x25\x26\x28\x29\x2a\x2b\x2c\x2d\x2f\x3a\x3b\x3c\x3d\x3e\x3f\x5b\x5d\x5c\x7b\x7c\x7d\x7e])\s/g, "$1");
  return s;
}

function combineLiteralStrings(s) {

  var i;

  s = s.replace(/"\+"/g, "");
  s = s.replace(/'\+'/g, "");

  return s;
}

function restoreLiteralStrings(s) {

  var i;

  for (i = 0; i < literalStrings.length; i++)
    s = s.replace(new RegExp("__" + i + "__"), literalStrings[i]);

  return s;
}

//]]></SCRIPT>

<META name=GENERATOR content="MSHTML 8.00.7600.16490"></HEAD>
<BODY>
<H3>JavaScript 压缩---<A class=STYLE2 
href="http://www.css88.com/">返回前端开发首页</A></H3>
<HR>

<DIV id=demoBox class=STYLE2>&nbsp;&nbsp;这程序是由Mike 
Hall写的，JavaScript的压缩不是为了保护代码而压缩，而是压缩后的js代码文件可以小一倍甚至多倍，从而使这个js代码快速的下载到客户端，特别js文件较大时速度效果非常明显． 
<BR><BR>使用方法：<BR>&nbsp;&nbsp;使用下面的表单,你可以浓缩JavaScript代码.只用将任何脚本代码粘贴到输入的文本框,压下 
'压缩' 按钮,那浓缩的版本就出现在 '输出' 里. 然后你可以使用 '全选' 
按钮,快速选中'输出'里的代码,剪切粘贴到一个新的源码文件.<BR><BR>3.JavaScript 格式化工具<A 
href="http://www.css88.com/tool/js_beautify/" 
target=_blank>http://www.css88.com/tool/js_beautify/</A></DIV>
<P></P>
<DIV style="WIDTH: 1003px">
<DIV style="FLOAT: left">
<FORM onsubmit="return crunch(this)" action="">
<TABLE border=0>
  <TBODY>
  <TR>
    <TH colSpan=3>输入</TH></TR>
  <TR>
    <TD colSpan=3><TEXTAREA rows=10 cols=80 name=codeIn></TEXTAREA></TD></TR>
  <TR>
    <TD><B>字节数:</B> <INPUT disabled size=10 type=text name=sizeIn></TD>
    <TD><B>状态:</B> <INPUT disabled size=30 type=text name=statusMsg></TD>
    <TD style="TEXT-ALIGN: right"><INPUT class=button value=压缩 type=submit> <INPUT class=button value=清除 type=reset></TD></TR></TBODY></TABLE>
<P></P>
<TABLE border=0>
  <TBODY>
  <TR>
    <TH colSpan=4>输出</TH></TR>
  <TR>
    <TD colSpan=4><TEXTAREA rows=10 cols=80 name=codeOut></TEXTAREA></TD></TR>
  <TR>
    <TD><B>字节数:</B> <INPUT disabled size=10 type=text name=sizeOut></TD>
    <TD><B>减少:</B> <INPUT disabled size=10 type=text name=sizeDiff></TD>
    <TD><B>% 减少率:</B> <INPUT disabled size=10 type=text name=pctOut></TD>
    <TD style="TEXT-ALIGN: right"><INPUT class=button onclick=this.form.codeOut.select();this.form.codeOut.focus(); value=全选 type=button></TD></TR></TBODY></TABLE></FORM></DIV>
<DIV style="WIDTH: 320px; FLOAT: right">
<P class=STYLE1>使用须知：</P>
<P class=STYLE1>大量的代码运行效率较低，cpu占用较高，请耐心等待！呵呵</P>
<P class=STYLE2>1.保存好您的开发版本，便于以后修改维护；</P>
<P class=STYLE2>2.压缩前，检查每一行代码确保以“;”结束;</P>
<P 
class=STYLE2>3.if...else...语句加上“{}”，即如果你的i语句为<BR>if(...)<BR>...//一条语句<BR>else<BR>...//一条语句</P>
<P 
class=STYLE2>请改为<BR>if(...)<BR>{...}//一条语句<BR>else<BR>{...}//一条语句</P></DIV></DIV><!--google统计-->
<SCRIPT type=text/javascript>
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</SCRIPT>

<SCRIPT type=text/javascript>
try {
var pageTracker = _gat._getTracker("UA-3448069-1");
pageTracker._trackPageview();
} catch(err) {}</SCRIPT>
</BODY></HTML>
